package com.ctms.common.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

public final class VerifyUtil {
    //验证码字符集
    private static final char[] chars = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    //字符数量
    private static final int SIZE = 4;
    //干扰线数量
    private static final int LINES = 10;
    //宽度
    private static final int WIDTH = 100;
    //高度
    private static final int HEIGHT = 32;
    //字体大小
    private static final int FONT_SIZE = 20;

    /*
    * 生成随机验证码及图片
    * 数组中【验证码，图片】
    **/
    public static Object[] createImage(){
        StringBuffer sb = new StringBuffer();
        //1、创建空白图片
        BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
        //2、获取图片画笔
        Graphics graphics = image.getGraphics();
        //3、设置画笔颜色
        graphics.setColor(Color.LIGHT_GRAY);
        //4、绘制矩形背景
        graphics.fillRect(0,0,WIDTH,HEIGHT);
        //5、画随机字符
        Random random = new Random();
        for (int i =0;i<SIZE;i++){
            //取随机字符索引
            int n = random.nextInt(chars.length);
            //设置随机颜色
            graphics.setColor(getRandomColor());
            //设置字体大小
            graphics.setFont(new Font(null,Font.BOLD + Font.ITALIC,FONT_SIZE));
            //画字符
            graphics.drawString(chars[n] + "",i*WIDTH / SIZE,HEIGHT/2);
            //记录字符
            sb.append(chars[n]);
        }
        //6、画干扰线
        for (int i =0;i<LINES;i++){
            graphics.setColor(getRandomColor());
            //随机画线
            graphics.drawLine(random.nextInt(WIDTH), random.nextInt(HEIGHT),
                    random.nextInt(WIDTH),random.nextInt(HEIGHT));
        }
        return new Object[]{sb.toString(),image};
    }
    //随机取色
    public static Color getRandomColor(){
        Random random = new Random();
        Color color = new Color(random.nextInt(256),random.nextInt(256),
                random.nextInt(256));
        return color;
    }

    public static void main(String[] args) throws IOException {
        Object[] objects = createImage();
        BufferedImage image = (BufferedImage) objects[1];
        OutputStream outputStream = new FileOutputStream("src/main/resources/static/img/verify.png");
        ImageIO.write(image,"png",outputStream);
        outputStream.close();
    }
}
